package com.tabnet.servlet.client;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tabnet.beans.commande.Commande;
import com.tabnet.beans.personnes.Client;
import com.tabnet.dao.DAOCommande;
import com.tabnet.dao.DAOException;
import com.tabnet.dao.DAOFactory;

public class DetailPanier extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
    public static final String SESSION_COMMANDE     = "commande";
    public static final String SESSION_USER 		= "sessionClient";
    public static final String VUE_SUCCES      		= "/adminClient/panier.jsp";
    public static final String VUE        			= "/index.jsp";
    
    private DAOCommande _DAOCommande;
    
    public void init() throws ServletException {
        /* Récupération d'une instance de notre DAO Utilisateur */
    	try {
			this._DAOCommande = DAOFactory.getInstance().getCommandeDAO();
		}
    	catch (DAOException e) {
			e.printStackTrace();
		}
    }
    
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

    	/* Récupération du client dans la session */
        HttpSession session = request.getSession();
        
    	Client oClient = (Client) session.getAttribute(SESSION_USER);
    	
    	if(oClient == null)
            this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
    	
    	try{
    		Commande commande = this._DAOCommande.getCommandeEnCours(oClient.getId());

	    	session.setAttribute( SESSION_COMMANDE, commande );
	    	session.setAttribute( SESSION_USER, oClient );
	    	
	    	this.getServletContext().getRequestDispatcher( VUE_SUCCES ).forward( request, response );
        }
        catch(DAOException e){
            this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
        }
    }

    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
    	this.doGet(request, response);
    }

}
